﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MyGame/Enemy")]

public class Enemy : MonoBehaviour {
    //移动速度
    public float m_speed = 1;
    //旋转速度
    public float m_rotSteed = 30;
    //变向间隔时间
    public float m_timer = 1.5f;

    protected Transform m_transform;
    //生命值
    public float m_life = 3;

    //声音
    public AudioClip m_shootClip;
    //声音源
    protected AudioSource m_audio;
    //爆炸特效
    public Transform m_explosionFX;

    public int m_point = 10;

    private  void Awake()
    {
        m_transform =this.transform;
        m_audio = this.m_transform.GetComponent<AudioSource>();

    }
    // Use this for initialization

    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (m_life<=0)
        {
            //爆炸特效
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);

            GameManager.Instance.AddScore(m_point);
            
            destroy();

        }
        UpdateMove();
	}
    private float del = 1;
    protected virtual void UpdateMove()
    {
        m_timer -= Time.deltaTime;

        if (m_timer<=0)
        {
            m_timer = 3;

            //改变旋转方向
            del = -del;

        }

        //旋转方向
        m_transform.Rotate(Vector3.up,m_rotSteed*Time.deltaTime*del,Space.World);

        //前进
        m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));

             

    }
    //碰撞
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Rocket")
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket!=null)
            {
                m_life -= rocket.m_power;
            }
        }
        else if (other.tag == "Player")
        {
            m_life = 0;
        }
    }
    void destroy()
    {
        Destroy(gameObject);
    }
    
}
